package pro.spring.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pro.spring.inter.UsersMapper;
import pro.spring.inter.UdiscussMapper;
import pro.spring.inter.UdiscusserMapper;
import pro.spring.inter.UsersHasThemeMapper;
import pro.spring.inter.ThemeMapper;
import pro.spring.model.Theme;
import pro.spring.model.Udiscuss;
import pro.spring.model.Udiscusser;
import pro.spring.model.UsersHasThemeKey;
import pro.spring.vo.PaginationVo;

@Service
public class ThemeService{

	@Autowired
	private ThemeMapper ThemeMapper;
	
	@Autowired
	private UsersMapper UsersMapper;
	
	@Autowired
	private UsersHasThemeMapper UsersHasThemeMapper;
	
	@Autowired
	private UdiscusserMapper UdiscusserMapper;
	
	@Autowired
	private UdiscussMapper UdiscussMapper;
	
	public int deleteByPrimaryKey(Integer id) {
		return ThemeMapper.deleteByPrimaryKey(id);
	}

	public int insert(Theme record) {
		return ThemeMapper.insert(record);
	}
	
	public int selectName(String name) {
		return ThemeMapper.selectName(name);
	}
	
	public int insertOnethem(Integer Uid,Theme th) {
		Udiscusser u = new Udiscusser();
		u.setDrContent(" ");
		u.setDrType(6);//主题讨论区
		u.setUsersUId(Uid);
		int a =UdiscusserMapper.insert(u);	
		Integer num = 0;
		if(a==1){
			int uid =UdiscusserMapper.selectNewId();
			th.setUdiscusserDrId(uid);
			num = ThemeMapper.insert(th);
			UsersHasThemeKey us = new UsersHasThemeKey();
			us.setThemeThId(ThemeMapper.selectByNew());
			us.setUsersUId(Uid);
			int h = UsersHasThemeMapper.insert(us);
			if(h==1){
				num = uid;
				Udiscuss ud = new Udiscuss();
				ud.setDsContent("<p>大家好！关于 "+th.getThName()+" 的主题讨论区建立啦！快来发现新朋友吧！</p>");
				ud.setUdiscusserDrId(uid);
				ud.setUsersUId(Uid);
				UdiscussMapper.insert(ud);
			}
		}else{
			num = 0;
		}
		return num;
	}

	public Theme selectByPrimaryKey(Integer id) {
		return ThemeMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKey(Theme record) {
		return ThemeMapper.updateByPrimaryKey(record);
	}
	
	public PaginationVo<Theme> getPaginationInfo(String key,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Theme> pageVo=new PaginationVo<Theme>();
		//一定要在setPageCount()方法之后执行setPageNum
		List<Theme> li =  null ;
		if(key == null || key == ""){
			pageVo.setCount(ThemeMapper.selectCount());
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			Integer offset = pageVo.getOffset();
			if(offset<0){
				offset = 0;
			}
			li=ThemeMapper.selectListPage(offset, pageVo.getRows());
		}else{
			pageVo.setCount(ThemeMapper.selectByKey(key));
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			li=ThemeMapper.selectListByKey(key,pageVo.getOffset(), pageVo.getRows());
		}
		for(Theme th:li){
			th.setUser(UsersMapper.selectByPrimaryKey(UdiscusserMapper.selectByPrimaryKey(th.getUdiscusserDrId()).getUsersUId()));
		}
		pageVo.setList(li);
		return pageVo;
	}
	
	//select list Theme by user
	public PaginationVo<Theme> getPaginationByuser(Integer uid,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Theme> pageVo=new PaginationVo<Theme>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(ThemeMapper.selectCountByUid(uid));
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		List<Theme> li = ThemeMapper.selectListUser(uid,pageVo.getOffset(), pageVo.getRows());
		for(Theme th:li){
			th.setUser(UsersMapper.selectByPrimaryKey(uid));
		}
		pageVo.setList(li);
		return pageVo;
	}
}
